home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8562 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  189 lines

  1. Newsgroups: comp.lang.c++
  2. Path: lut.ac.uk!usenet
  3. From: COSJM1 <S.J.Morgan-92@student.lut.ac.uk>
  4. Subject: Boring template problem again
  5. Sender: usenet@lut.ac.uk (Usenet-News)
  6. Message-ID: <DMzpFy.Mo8@lut.ac.uk>
  7. Date: Sun, 18 Feb 1996 21:09:34 GMT
  8. X-Nntp-Posting-Host: co-pclab27.lut.ac.uk
  9. Content-Transfer-Encoding: 7bit
  10. Content-Type: text/plain
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
  13. Organization: Loughborough University
  14.  
  15. Hi,
  16.     With reference to my previous post, thanks for the replies, the
  17. trouble is, they seem to provide various levels of success but none of them means the code links correctly under CFront 3.0 and GCC 2.7.0.
  18.  
  19. To reinterate my problem, given the following:
  20.  
  21. IN FILE "template.h":
  22.  
  23. template<class T> class B
  24. {
  25. public:
  26.     B() {}
  27.     void test(void);
  28. };
  29.  
  30. IN FILE "template1.cpp":
  31.  
  32. #include "template.h"
  33.  
  34. void test_again( void );
  35.  
  36. main()
  37. {
  38. B<int*>
  39.     test;
  40.  
  41.     test.test();
  42.  
  43.     test_again();
  44. }
  45.  
  46. IN FILE "template2.cpp":
  47.  
  48. #include "template.h"
  49.  
  50. void test_again( void )
  51. {
  52. B<int*>
  53.     test;
  54.  
  55.     test.test();
  56. }
  57.  
  58. IN FILE "implem.cpp":
  59.  
  60. #include <iostream.h>
  61.  
  62. #include "template.h"
  63.  
  64. template<class T> void B<T>::test(
  65.         void )
  66. {
  67.     cout << "hello";
  68. }
  69.  
  70. the linker reports the following error:
  71.  
  72. (Error) Undefined symbol(s)
  73.     B<int*>::test(void), referred to from template1.o 
  74.  
  75. It was suggested that I include the member function definition after the
  76. class as below:
  77.  
  78. IN FILE "template.h":
  79.  
  80. #include <iostream.h>
  81.  
  82. template<class T> class B
  83. {
  84. public:
  85.     B() {}
  86.     void test(void);
  87. };
  88.  
  89. template<class T> void B<T>::test(
  90.         void )
  91. {
  92.     cout << "hello";
  93. }
  94.  
  95. IN FILE "template1.cpp"::
  96.  
  97. #include "template.h"
  98.  
  99. void test_again( void );
  100.  
  101. main()
  102. {
  103. B<int*>
  104.     test;
  105.  
  106.     test.test();
  107.  
  108.     test_again();
  109. }
  110.  
  111. IN FILE "template2.cpp"::
  112.  
  113. #include "template.h"
  114.  
  115. void test_again( void )
  116. {
  117. B<int*>
  118.     test;
  119.  
  120.     test.test();
  121. }
  122.  
  123. This time the following link error reported is:
  124.  
  125. (Error) Global test__11B__pt__3_PiFV multiply defined (in template2 and template1)
  126.  
  127. by CFront 3.0, but is linked correctly under GCC(g++) 2.7.0.
  128.  
  129. If I do the following, it links under CFront but not GCC:
  130.  
  131. IN FILE "template.h":
  132.  
  133. template<class T> class B
  134. {
  135. public:
  136.     B() {}
  137.     void test(void);
  138. };
  139.  
  140. // force instantiation
  141. extern B<int*> __test__;
  142.  
  143. IN FILE "template1.cpp":
  144.  
  145. #include "template.h"
  146.  
  147. void test_again( void );
  148.  
  149. main()
  150. {
  151. B<int*>
  152.     test;
  153.  
  154.     test.test();
  155.  
  156.     test_again();
  157. }
  158.  
  159. IN FILE "template2.cpp":
  160.  
  161. #include "template.h"
  162.  
  163. void test_again( void )
  164. {
  165. B<int*>
  166.     test;
  167.  
  168.     test.test();
  169. }
  170.  
  171. IN FILE "implem.cpp":
  172.  
  173. #include <iostream.h>
  174.  
  175. #include "template.h"
  176.  
  177. template<class T> void B<T>::test(
  178.         void )
  179. {
  180.     cout << "hello";
  181. }
  182.  
  183. The trouble is the code must be portable - does anybody know the best
  184. approach I should follow.
  185.  
  186. Thank you again for any advice given.
  187.  
  188.  
  189.